package com.frikinet.lifeguard.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.events.PostSelectedEvent;
import com.frikinet.lifeguard.events.PostSelectedListener;
import com.frikinet.lifeguard.model.Post;

public class PostNavigation extends JPanel implements PostSelectedListener
{
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory.getLogger(PostViewer.class);
	
	private Font clickFont, currentFont;
	
	private JLabel previousPost, nextPost;
	private JLabel currentPost;
	
	private Post post;
	
	public PostNavigation()
	{
		super();
		
		App.events.addPostSelectedListener(this);
		
		Font defaultFont = UIManager.getDefaults().getFont("Label.font");
		clickFont = new Font(defaultFont.getName(), Font.BOLD, defaultFont.getSize()+2);
		currentFont = new Font(defaultFont.getName(), defaultFont.getStyle(), defaultFont.getSize()+2);
		
		
		buildGUI();
	}
	
	private void buildGUI()
	{
		setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		
		JPanel west = new JPanel(new FlowLayout(FlowLayout.LEFT));
		previousPost = setupClickLabel(PostNavigationType.PREVIOUS);
		west.add(previousPost);
		constraints.weightx = 1.0;
		layout.setConstraints(west, constraints);
		add(west);
		
		JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
		currentPost = new JLabel(" ");
		currentPost.setFont(currentFont);
		center.add(currentPost);
		constraints.weightx = 3.0;
		layout.setConstraints(center, constraints);
		add(center);
		
		JPanel east = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		nextPost = setupClickLabel(PostNavigationType.NEXT);
		east.add(nextPost);
		constraints.weightx = 1.0;
		layout.setConstraints(east, constraints);
		add(east);
	}
	
	private JLabel setupClickLabel(PostNavigationType type)
	{
		JLabel label = new JLabel(" ");
		label.setFont(clickFont);
		label.setForeground(Color.blue);
		label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label.addMouseListener(new PostNavigationListener(type));
		return label;
	}
	
	@Override
	public void postSelected(PostSelectedEvent event)
	{
		this.post = event.getPost();
		
		currentPost.setText(App.i18n.translate("comp.post.navigation.post.current", post.getDate()));
		
		int index = post.getUser().getPosts().indexOf(post);
		
		if(index > 0)
		{
			Post previous = post.getUser().getPosts().get(index-1);
			previousPost.setText(String.format("< %s", App.i18n.translate("comp.post.navigation.post.click", previous.getDate())));
		} 
		else
		{
			previousPost.setText(" ");
		}
		
		if(index < post.getUser().getPosts().size() - 1)
		{
			Post next = post.getUser().getPosts().get(index + 1);
			nextPost.setText(String.format("%s >", App.i18n.translate("comp.post.navigation.post.click", next.getDate())));
		}
		else
		{
			nextPost.setText(" ");
		}
	}
	
	private enum PostNavigationType
	{
		PREVIOUS, NEXT;
	}
	
	private class PostNavigationListener extends MouseAdapter
	{
		private PostNavigationType type;
		
		private PostNavigationListener(PostNavigationType type)
		{
			this.type = type;
		}

		@Override
		public void mouseReleased(MouseEvent e)
		{
			log.trace(String.format("Post Navigation button clicked: %s", type));
			Post post = PostNavigation.this.post;
			int index = post.getUser().getPosts().indexOf(post);
			if(type==PostNavigationType.PREVIOUS)
				post = post.getUser().getPosts().get(index - 1);
			else
				post = post.getUser().getPosts().get(index + 1);
			App.events.firePostSelected(new PostSelectedEvent(this,post));
		}
	}
}
